/*
 *    This file is part of SSHTunneler.
 *
 *    Copyright 2009 Jean-Sebastien Gelinas <calestar@gmail.com>
 *
 *    SSHTunneler is free software: you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation, either version 3 of the License, or
 *    (at your option) any later version.
 *
 *    SSHTunneler is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with SSHTunneler.  If not, see <http://www.gnu.org/licenses/>.
 *
 */
package sshtunneler.server_tunnel.gui;

import java.awt.Color;
import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.KeyStroke;

import sshtunneler.library.Server;
import sshtunneler.server_tunnel.tree.GenericServerTunnelTree;

public class AddServer extends javax.swing.JDialog {
	private static final long serialVersionUID = -2533106961709111071L;
	private Server this_server;
	private GenericServerTunnelTree tree;

	public AddServer(JFrame parent, Server this_server,
			GenericServerTunnelTree tree) {
		super(parent);
		this.initComponents();
		this.tree = tree;
		this.rootPane.setDefaultButton(this.cmd_add);
		this.this_server = this_server;
		if (this_server != null) {
			this.cmd_add.setText("Modify");
			this.txt_port.setText("" + this_server.getPort());
			this.txt_username.setText(this_server.getUsername());
			this.txt_hostname.setText(this_server.getHostname());
		}
		Action close = new AbstractAction() {
			private static final long serialVersionUID = -1560933746603842265L;

			public void actionPerformed(ActionEvent e) {
				AddServer.this.cmd_cancelActionPerformed(null);
			}
		};
		this.getRootPane().getInputMap().put(
				KeyStroke.getKeyStroke(java.awt.Event.ESCAPE, 0), "close");
		this.getRootPane().getActionMap().put("close", close);
	}

	private void initComponents() {

		this.jPanel1 = new javax.swing.JPanel();
		this.jLabel1 = new javax.swing.JLabel();
		this.txt_hostname = new javax.swing.JTextField();
		this.jPanel3 = new javax.swing.JPanel();
		this.jLabel3 = new javax.swing.JLabel();
		this.txt_username = new javax.swing.JTextField();
		this.jPanel2 = new javax.swing.JPanel();
		this.jLabel2 = new javax.swing.JLabel();
		this.txt_port = new javax.swing.JTextField();
		this.jPanel4 = new javax.swing.JPanel();
		this.cmd_add = new javax.swing.JButton();
		this.cmd_cancel = new javax.swing.JButton();

		this
				.setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
		this.setTitle("Add server");
		this.setMinimumSize(new java.awt.Dimension(300, 160));
		this.setModal(true);
		this.setName("Form");
		this.setResizable(false);
		this.getContentPane().setLayout(
				new javax.swing.BoxLayout(this.getContentPane(),
						javax.swing.BoxLayout.PAGE_AXIS));

		this.jPanel1.setMaximumSize(new java.awt.Dimension(200, 30));
		this.jPanel1.setName("jPanel1");

		this.jLabel1.setText("Hostname:");
		this.jLabel1.setName("jLabel1");
		this.jPanel1.add(this.jLabel1);

		this.txt_hostname.setMaximumSize(new java.awt.Dimension(200, 20));
		this.txt_hostname.setMinimumSize(new java.awt.Dimension(100, 20));
		this.txt_hostname.setName("txt_hostname");
		this.txt_hostname.setPreferredSize(new java.awt.Dimension(100, 20));
		this.jPanel1.add(this.txt_hostname);

		this.getContentPane().add(this.jPanel1);

		this.jPanel3.setMaximumSize(new java.awt.Dimension(200, 30));
		this.jPanel3.setName("jPanel3");

		this.jLabel3.setText("Username:");
		this.jLabel3.setName("jLabel3");
		this.jPanel3.add(this.jLabel3);

		this.txt_username
				.setToolTipText("Leave the username blank to use the default username");
		this.txt_username.setMaximumSize(new java.awt.Dimension(200, 6));
		this.txt_username.setMinimumSize(new java.awt.Dimension(100, 20));
		this.txt_username.setName("txt_username");
		this.txt_username.setPreferredSize(new java.awt.Dimension(100, 20));
		this.jPanel3.add(this.txt_username);

		this.getContentPane().add(this.jPanel3);

		this.jPanel2.setMaximumSize(new java.awt.Dimension(200, 30));
		this.jPanel2.setName("jPanel2");

		this.jLabel2.setText("Port:");
		this.jLabel2.setName("jLabel2");
		this.jPanel2.add(this.jLabel2);

		this.txt_port.setText("22");
		this.txt_port.setMaximumSize(new java.awt.Dimension(200, 6));
		this.txt_port.setMinimumSize(new java.awt.Dimension(20, 20));
		this.txt_port.setName("txt_port");
		this.txt_port.setPreferredSize(new java.awt.Dimension(50, 20));
		this.jPanel2.add(this.txt_port);

		this.getContentPane().add(this.jPanel2);

		this.jPanel4.setMaximumSize(new java.awt.Dimension(200, 38));
		this.jPanel4.setMinimumSize(new java.awt.Dimension(131, 38));
		this.jPanel4.setName("jPanel4");

		this.cmd_add.setText("Add");
		this.cmd_add.setName("cmd_add");
		this.cmd_add.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				AddServer.this.cmd_addActionPerformed(evt);
			}
		});
		this.jPanel4.add(this.cmd_add);

		this.cmd_cancel.setText("Cancel");
		this.cmd_cancel.setName("cmd_cancel");
		this.cmd_cancel.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				AddServer.this.cmd_cancelActionPerformed(evt);
			}
		});
		this.jPanel4.add(this.cmd_cancel);

		this.getContentPane().add(this.jPanel4);
	}

	private void cmd_addActionPerformed(java.awt.event.ActionEvent evt) {
		String username = this.txt_username.getText();
		String hostname = this.txt_hostname.getText();
		String str_port = this.txt_port.getText();
		int port;

		this.txt_hostname
				.setBorder(BorderFactory.createLineBorder(Color.BLACK));
		this.txt_port.setBorder(BorderFactory.createLineBorder(Color.BLACK));

		if (hostname == null || hostname.equals("")) {
			this.txt_hostname.setBorder(BorderFactory.createLineBorder(
					Color.RED, 2));
			JOptionPane.showMessageDialog(this, "All fields are mandatory.",
					"Missing information", JOptionPane.ERROR_MESSAGE);
			return;
		}
		if (str_port == null || str_port.equals("")) {
			this.txt_port.setBorder(BorderFactory
					.createLineBorder(Color.RED, 2));
			JOptionPane.showMessageDialog(this, "All fields are mandatory.",
					"Missing information", JOptionPane.ERROR_MESSAGE);
			return;
		}
		try {
			port = Integer.parseInt(str_port);
		} catch (NumberFormatException e) {
			this.txt_port.setBorder(BorderFactory
					.createLineBorder(Color.RED, 2));
			JOptionPane.showMessageDialog(this,
					"The port should be a number !", "Not a number",
					JOptionPane.ERROR_MESSAGE);
			return;
		}
		if (this.this_server != null) {
			this.this_server.setPort(port);
			this.this_server.setUsername(username);
			this.this_server.setHostname(hostname);
			this.tree.process_server_action(false, false, true);
		} else {
			this.tree.create_server(username, hostname, port);
			this.tree.process_server_action(true, false, false);
		}

		this.dispose();
	}

	private void cmd_cancelActionPerformed(java.awt.event.ActionEvent evt) {
		this.dispose();
	}

	private javax.swing.JButton cmd_add;
	private javax.swing.JButton cmd_cancel;
	private javax.swing.JLabel jLabel1;
	private javax.swing.JLabel jLabel2;
	private javax.swing.JLabel jLabel3;
	private javax.swing.JPanel jPanel1;
	private javax.swing.JPanel jPanel2;
	private javax.swing.JPanel jPanel3;
	private javax.swing.JPanel jPanel4;
	private javax.swing.JTextField txt_hostname;
	private javax.swing.JTextField txt_port;
	private javax.swing.JTextField txt_username;

}
